/**
 * 
 */
package com.java.test.system.mapper;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import com.java.test.system.dto.RoleInfo;

/**
 * @author
 *
 */
@Component
public class RoleMapper {

	@Autowired
	private JdbcTemplate jdbcTemplate;

	public List<RoleInfo> selectroleList(RoleInfo info) {

		String sql = "select * from system_role where 1=1";

		if (info.getCreateTime() != null) {
			sql += " and TO_DAYS(create_time)= TO_DAYS('" + info.getCreateTime() + "')";
		}

		if (info.getRoleName() != null && !"".equals(info.getRoleName())) {
			sql += " and role_name like '%" + info.getRoleName() + "%'";
		}

		List<RoleInfo> list = jdbcTemplate.query(sql, new RoleInfo());

		return list;

	}

	public int insertroleInfo(RoleInfo info) {
		String sql = "insert into system_role(role_name,power,create_time,add_people)values(?,?,?,?)";

		int i = jdbcTemplate.update(sql, info.getRoleName(), info.getPower(), info.getCreateTime(),
				info.getAddPeople());
		return i;
	}

	public RoleInfo selectrolebyId(RoleInfo info) {

		String sql = "select * from system_role where id=?";
		RoleInfo obj = null;
		try {
			obj = jdbcTemplate.queryForObject(sql, new RoleInfo(), info.getId());
		} catch (EmptyResultDataAccessException e) {
			return null;
		}

		return obj;

	}

	public int updateroleInfo(RoleInfo info) {

		String sql = "update system_role set role_name=?,power=? where id=?";

		int i = jdbcTemplate.update(sql, info.getRoleName(), info.getPower(), info.getId());

		return i;

	}

	public int deleteroleInfo(RoleInfo info) {

		String sql = "delete from system_role where id=?";

		int i = jdbcTemplate.update(sql, info.getId());

		return i;
	}

}
